/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExibirPerfil.java
 *
 * Created on 26/05/2011, 04:58:51
 */
package pitaqueiro;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lucas
 */
public class ExibirPerfil extends javax.swing.JFrame {
    
    
    int IndiceUsuario ;
    String NomeUsuario;
    /** Creates new form ExibirPerfil */
    public ExibirPerfil() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        List<String> nomeUsuario = new ArrayList<String>();
        ManipulacaoDeArquivo leArq = new ManipulacaoDeArquivo();

        try{
            leArq.leEstabelecimentos();
            leArq.leOpinioes();
            List<Estabelecimento> listuaEstabelecimento = leArq.getListaEstabelecimento();
            for(PerfilUsuario use: leArq.getListaPerfilUsuarioSistema()){
                nomeUsuario.add(use.getNome());
            }
        }catch (Exception ex){
            ex.getMessage();
        }
        Usuarios = new javax.swing.JComboBox();
        PainelExibir = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ManipulacaoDeArquivo LeArq = new ManipulacaoDeArquivo();
        String [] cabesalho = {"Estabelecimento","Avaliacao"};
        String matriz[][]=null;
        try{
            LeArq.leEstabelecimentos();
            LeArq.leOpinioes();
            List<String> listaEstabelecimento = LeArq.getListaRestaurantes();
            List<PerfilUsuario> listaUsuario = LeArq.getListaPerfilUsuarioSistema();
            matriz = new String[listaUsuario.size()][1];
            for(int i = 0; i < listaUsuario.size();i++){
                if(listaUsuario.get(i).getNome().equals(NomeUsuario)){
                    matriz[i][0] = listaEstabelecimento.get(i);
                    matriz[i][1] = listaUsuario.get(i).getMapaOpinioes().get(listaEstabelecimento.get(i));
                }
                matriz[i][0] = null;
                matriz[i][1] = null;

            }
        }catch (Exception ex){
            ex.getMessage();
        }
        TabelaPerfil = new javax.swing.JTable();
        BotaoExibir = new javax.swing.JButton();
        BotaoVolar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Exibir perfil");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Usuario: ");

        Usuarios.setModel(new javax.swing.DefaultComboBoxModel(nomeUsuario.toArray()));
        Usuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UsuariosActionPerformed(evt);
            }
        });

        PainelExibir.setVisible(false);

        TabelaPerfil.setModel(new javax.swing.table.DefaultTableModel(
            matriz,cabesalho
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TabelaPerfil);

        javax.swing.GroupLayout PainelExibirLayout = new javax.swing.GroupLayout(PainelExibir);
        PainelExibir.setLayout(PainelExibirLayout);
        PainelExibirLayout.setHorizontalGroup(
            PainelExibirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PainelExibirLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                .addContainerGap())
        );
        PainelExibirLayout.setVerticalGroup(
            PainelExibirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PainelExibirLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        BotaoExibir.setText("Exibir");
        BotaoExibir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoExibirActionPerformed(evt);
            }
        });

        BotaoVolar.setText("Voltar");
        BotaoVolar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoVolarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Usuarios, 0, 415, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(PainelExibir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(246, Short.MAX_VALUE)
                        .addComponent(BotaoVolar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotaoExibir, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BotaoExibir, BotaoVolar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Usuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PainelExibir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotaoExibir)
                    .addComponent(BotaoVolar))
                .addContainerGap(290, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void UsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UsuariosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_UsuariosActionPerformed

    private void BotaoExibirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoExibirActionPerformed
        
        IndiceUsuario = Usuarios.getSelectedIndex();
        NomeUsuario = DarNomeUsuario(IndiceUsuario);
        
        /*
         * Exibe o perfil
         */

        ManipulacaoDeArquivo LeArq = new ManipulacaoDeArquivo();
        String [] cabesalho = {"Estabelecimento","Avaliacao"};
        String matriz[][]=null;
        PerfilUsuario UsuarioDaVez=null;
        try{
        LeArq.leEstabelecimentos();
        LeArq.leOpinioes();
        List<String> listaEstabelecimento = LeArq.getListaRestaurantes();
        List<PerfilUsuario> listaUsuario = LeArq.getListaPerfilUsuarioSistema();
        matriz = new String[listaEstabelecimento.size()][2];
        
        for(PerfilUsuario usuario:listaUsuario ){
            if(usuario.getNome().equals(NomeUsuario)){
                UsuarioDaVez = usuario;
            }
        }
        
        for(int i = 0; i < listaEstabelecimento.size();i++){
                matriz[i][0] = listaEstabelecimento.get(i);//listaEstabelecimento.get(i);
                matriz[i][1] = UsuarioDaVez.getMapaOpinioes().get(listaEstabelecimento.get(i));
              
        }
        }catch (Exception ex){
            ex.getMessage();
        }
        TabelaPerfil = new javax.swing.JTable();

        TabelaPerfil.setModel(new javax.swing.table.DefaultTableModel(
            matriz,cabesalho
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });



jScrollPane1.setViewportView(TabelaPerfil);
        
        
        System.out.println(UsuarioDaVez.getMapaOpinioes());
        PainelExibir.setVisible(true);
       
    }//GEN-LAST:event_BotaoExibirActionPerformed

    private void BotaoVolarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoVolarActionPerformed
        new Menu().show();
        this.dispose();
    }//GEN-LAST:event_BotaoVolarActionPerformed
    
    
    public String DarNomeUsuario(int indice) {
        String nome=null;
        ManipulacaoDeArquivo manipula2 = new ManipulacaoDeArquivo();
        try {
            manipula2.leEstabelecimentos();
        } catch (IOException ex) {
            Logger.getLogger(ExibirPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            manipula2.leOpinioes();
        } catch (IOException ex) {
            Logger.getLogger(ExibirPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i=0;i<manipula2.getListaPerfilUsuarioSistema().size();i++){
           
            if(i==indice){
                nome=manipula2.getListaPerfilUsuarioSistema().get(i).getNome();
                
            }
                   
        }
        return nome;
        
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ExibirPerfil().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotaoExibir;
    private javax.swing.JButton BotaoVolar;
    private javax.swing.JPanel PainelExibir;
    private javax.swing.JTable TabelaPerfil;
    private javax.swing.JComboBox Usuarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
